package utils

import (
	"sync"
	"sync/atomic"
)

type MultiGroupTaskSignRec struct {
	id          interface{}
	groupid     interface{}
	cb          func(sender ...interface{})
	ownerPool   *sync.Pool
	releaseflag int32
}

var (
	sign_rec_pool = NewSyncPool(func() interface{} {
		return &MultiGroupTaskSignRec{releaseflag: 0}
	})
	sign_rec_debug_cnt int32 = 0
)

func getSignRec() *MultiGroupTaskSignRec {
	rval := sign_rec_pool.Get().(*MultiGroupTaskSignRec)
	rval.ownerPool = sign_rec_pool
	rval.releaseflag = 1
	atomic.AddInt32(&sign_rec_debug_cnt, 0)
	return rval
}

/**
 * 使用完后进行检测
 */
func CheckPoolStatus_MultiGroupTaskSignRec() bool {
	return sign_rec_debug_cnt == 0
}

func (this *MultiGroupTaskSignRec) Release() {
	if atomic.CompareAndSwapInt32(&this.releaseflag, 1, 0) {
		this.ownerPool.Put(this)
		atomic.AddInt32(&sign_rec_debug_cnt, -1)
	} else {
		panic("[BUG]多次归还")
	}
}
